var postForm;
$(function () {

    var columns = [
        {
            field: 'ck',
            checkbox: true
        },
        {
            field: 'postName',
            title: '岗位名称'
        },
        {
            field: 'postCode',
            title: '岗位名称'
        },
        {
            field: 'postSort',
            title: '排序'
        }, {
            field: 'status',
            title: '状态',
            align: 'center',
            formatter: function (value, row, index) {
                if (row.status == null || row.status == '1') {
                    return '<button id="btn_edit" type="button" class="btn btn-warning disabled btn-xs" >停用</button>'
                } else {
                    return '<button id="btn_edit" type="button" class="btn btn-success disabled btn-xs" >启用</button>'
                }
            }
        },
        {
            field: 'createDate',
            title: '创建时间',
            formatter: function (value, row, index) {
                return changeDateFormat("yyyy-MM-dd hh:mm:ss", new Date(value))
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row, index) {
                return '<button id="btn_edit" type="button" class="btn btn-primary  btn-xs" onclick="update(\'' + value + '\')"><i class="fa fa-edit"></i> 修改</button>'+
                    '<button type="button" class="btn btn-danger  btn-xs" onclick="deletes(\'' + value + '\')" style="margin-left: 10px;"><i class="fa fa-trash-o"></i> 删除</button>';
            }
        }
    ]
    createBootstrapTable($("#table"), '/sys/sysPost/info', columns, true, 'toolbar', 'server')

})

// 清空表单
function resetForm() {
    $("#postName").val("")
    $("#postCode").val("")
    $("#postSort").val("")
    $("#remarks").val("")
}

function add() {
    resetForm()
    postForm = layer.open({
        type: 1,
        area: (window.innerWidth>992)? "600px":"auto",
        title: '新增',
        shade: 0.6,
        anim: 1,
        content: $("#post_form"),
        success: function (layero, index) {
            layer.iframeAuto(index);
        }
    });
}

function save() {
    var id = $("#postId").val()
    var postName = $("#postName").val();
    var postCode = $("#postCode").val();
    var postSort = $("#postSort").val();
    var remarks = $("#remarks").val();
    var status = $("input[name='status']:checked").val();

    if (postName == '' || postName  == null) {
        layer.msg("请输入名称！")
        return
    }
    if (postCode == '' || postCode == null) {
        layer.msg("请输入编码！")
        return
    }

    var data = {
        id: id,
        postName: postName,
        postCode: postCode,
        postSort: postSort,
        status:status,
        remarks: remarks
    }
    request('/sys/sysPost/save', 1, data, function (res) {
        if (res.code == "0") {
            layer.close(postForm)
            layer.msg("保存成功", {time: 1000}, function () {
                onLoads($("#href").val())
            })
        }else {
            layer.msg(res.msg, {time: 1000,icon:5})
        }
    }, function (error) {
        layer.msg("系统异常！")
    })
}

function update(id) {
    resetForm()
    var data = {
        id: id
    }

    request('/sys/sysPost/getById', 0, data, function (data) {
        $("#postId").val(data.id)
        $("#postName").val(data.postName)
        $("#postCode").val(data.postCode)
        $("#postSort").val(data.postSort)
        $("#remarks").val(data.remarks)
        var r = data.status
        $("input[name='status'][value=" + r + "]").attr("checked", true);
    }, function (error) {
        layer.msg("系统异常！")
    })
    postForm = layer.open({
        type: 1,
        area: (window.innerWidth>992)? "600px":"auto",
        title: '修改',
        shade: 0.6,
        anim: 1,
        content: $("#post_form"),
        success: function (layero, index) {
            layer.iframeAuto(index);
        }
    });
}

function deletes(id) {

    var ids = [];
    if (id != undefined) {
        ids.push(id)
    } else {
        $.each($('tbody input:checkbox:checked'), function () {
            ids.push($(this).parents('tr').data('uniqueid'));
        });
    }

    if (ids.length < 1) {
        layer.msg("最少请选择一条数据！")
        return;
    }
    var data = {
        ids: ids.toString()
    }

    layer.confirm('确定删除吗？', {
        title:'提示',btn: ['取消', '确定'] //按钮
    }, function () {
        layer.close(layer.index)
    }, function () {
        request('/sys/sysPost/delete', 1, data, function (res) {
            if (res.code == 0) {
                layer.msg('删除成功', {time: 1000}, function () {
                    onLoads($("#href").val())
                })
            } else {
                layer.msg(res.msg, {time: 1000})
            }

        }, function (error) {
            layer.msg("系统异常！")
        })
    });
}

function searchForm() {
    var searchName = $("#searchName").val()

    var data = {
        postName: searchName
    }

    request('/sys/sysPost/info', 1, data, function (res) {
        $("#table").bootstrapTable('load', res);
    }, function (error) {
        layer.msg(error.msg)
    })
}

function restForm() {
    request('/sys/sysPost/info', 1, null, function (res) {
        $("#table").bootstrapTable('load', res);
    }, function (error) {
        layer.msg(error.msg)
    })
}